package com.example.ice.fittime.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.example.ice.fittime.R;
import com.example.ice.fittime.entry.UserActivity;

import java.util.List;

/**
 * Created by Administrator on 2016/10/14.
 */

public class HomeBBSRecyclerAdapter extends RecyclerView.Adapter<HomeBBSRecyclerAdapter.ViewHolder>{
    private Context context;
    private List<UserActivity> userActivityList;

    public HomeBBSRecyclerAdapter(Context context, List<UserActivity> userActivityList) {
        this.context = context;
        this.userActivityList = userActivityList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.home_bbs_sub, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        UserActivity userActivity = userActivityList.get(position);
        Glide.with(context).load(userActivity.getUserImage().getUrl()).into(holder.bbsImg);
        Glide.with(context).load(userActivity.getUserDetail().getAvatar_url()).asBitmap().into(new BitmapImageViewTarget(holder.bbsUserIcon) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                holder.bbsUserIcon.setImageDrawable(circularBitmapDrawable);
            }
        });
        holder.bbsUserName.setText(userActivity.getUserDetail().getName());
    }

    @Override
    public int getItemCount() {
        return userActivityList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView bbsImg;
        private final ImageView bbsUserIcon;
        private final TextView bbsUserName;

        public ViewHolder(View itemView) {
            super(itemView);
            bbsImg = ((ImageView) itemView.findViewById(R.id.home_bbs_img));
            bbsUserIcon = ((ImageView) itemView.findViewById(R.id.home_bbs_userIcon));
            bbsUserName = ((TextView) itemView.findViewById(R.id.home_bbs_userName));
        }
    }
}
